package csc.fresher.spring.dao;

import java.util.Date;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import csc.fresher.spring.domain.Employee;

public class EmployeeDAOImpl implements EmployeeDAO {

	private DataSource dataSource;
	private JdbcTemplate jdbcTemplateObject;

	@Override
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
		this.jdbcTemplateObject = new JdbcTemplate(dataSource);
	}

	@Override
	public void insertEmployee(String name, int age, String position,
			String phone, String address, Date dateJoin) {
		String SQL = "insert into employee(name, age, position, phone, address, dateJoin) values (?, ?, ?, ?, ?, ?)";
		jdbcTemplateObject.update(SQL, name, age, position, phone, address,
				dateJoin);
		System.out.println("Insert new employee with name: " + name + " age: "
				+ age + " position: " + position + " phone: " + phone
				+ " address: " + address + " dateJoin: " + dateJoin);
	}

	@Override
	public Employee getEmployee(int id) {
		try {
			String SQL = "select * from employee where id=?";
			Employee e = jdbcTemplateObject.queryForObject(SQL,
					new Object[] { id }, new EmployeeMapper());
			System.out.println("Select employee with id = " + id);
			return e;
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			System.out.println("Employee not exist");
			return null;
		}
	}

	@Override
	public List<Employee> listEmployee() {
		String SQL = "select * from employee";
		List<Employee> list = jdbcTemplateObject.query(SQL,
				new EmployeeMapper());
		System.out.println("Select list employee");
		return list;
	}

	@Override
	public void delete(int id) {
		try {
			String SQL = "delete from employee where id = ?";
			jdbcTemplateObject.update(SQL, id);
			System.out.println("Delete employee with id = " + id);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			System.out.println("Employee not exist");
		}

	}

	@Override
	public void updateName(int id, String name) {
		try {
			String SQL = "update employee set name = ? where id = ?";
			jdbcTemplateObject.update(SQL, name, id);
			System.out.println("Update employee id = " + id + " with new name: " + name);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			System.out.println("Employee not exist");
		}

	}

	@Override
	public void updateAge(int id, int age) {
		try {
			String SQL = "update employee set age = ? where id = ?";
			jdbcTemplateObject.update(SQL, age, id);
			System.out.println("Update employee id = " + id + " with new age: " + age);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			System.out.println("Employee not exist");
		}
	}

	@Override
	public void updatePosition(int id, String position) {
		try {
			String SQL = "update employee set position = ? where id = ?";
			jdbcTemplateObject.update(SQL, position, id);
			System.out.println("Update employee id = " + id + " with new position: " + position);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			System.out.println("Employee not exist");
		}
	}

	@Override
	public void updatePhone(int id, String phone) {
		try {
			String SQL = "update employee set phone = ? where id = ?";
			jdbcTemplateObject.update(SQL, phone, id);
			System.out.println("Update employee id = " + id + " with new phone: " + phone);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			System.out.println("Employee not exist");
		}
	}

	@Override
	public void updateAddress(int id, String address) {
		try {
			String SQL = "update employee set address = ? where id = ?";
			jdbcTemplateObject.update(SQL, address, id);
			System.out.println("Update employee id = " + id + " with new address: " + address);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			System.out.println("Employee not exist");
		}
	}

	@Override
	public void updateDateJoin(int id, Date date) {
		try {
			String SQL = "update employee set dateJoin = ? where id = ?";
			jdbcTemplateObject.update(SQL, date, id);
			System.out.println("Update employee id = " + id + " with new date: " + date);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			System.out.println("Employee not exist");
		}
	}

}
